package com.ruoyi.system.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CustomerPoints;
import com.ruoyi.system.service.ICustomerPointsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户积分Controller
 * 
 * @author LK
 * @date 2024-06-22
 */
@RestController
@RequestMapping("/system/customer_points")
public class CustomerPointsController extends BaseController
{
    @Autowired
    private ICustomerPointsService customerPointsService;

    /**
     * 查询客户积分列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer_points:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerPoints customerPoints)
    {
        startPage();
        // customerPoints里面只设置了ID
        List<CustomerPoints> list = customerPointsService.selectCustomerPointsList(customerPoints);
        // 若无积分记录，则添加默认值
        if(list.size()==0){
            customerPoints.setCustomerId(customerPoints.getCustomerId());
            customerPoints.setPointsChange("0");
            customerPoints.setTotalPoints("0");
            LocalDateTime now = LocalDateTime.now();
//            System.out.println(" 当前时间： " + now);

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd  HH:mm:ss");
            String currentTimeStr = now.format(formatter);
//            System.out.println(" 当前时间的字符串：" + currentTimeStr);
            customerPoints.setPointsChangeTime(String.valueOf(currentTimeStr));
            customerPointsService.insertCustomerPoints(customerPoints);
        }
//       再次查询
        list = customerPointsService.selectCustomerPointsList(customerPoints);
        return getDataTable(list);
    }
//
//    /**
//     * 导出客户积分列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:customer_points:export')")
//    @Log(title = "客户积分", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, CustomerPoints customerPoints)
//    {
//        List<CustomerPoints> list = customerPointsService.selectCustomerPointsList(customerPoints);
//        ExcelUtil<CustomerPoints> util = new ExcelUtil<CustomerPoints>(CustomerPoints.class);
//        util.exportExcel(response, list, "客户积分数据");
//    }
//
//    /**
//     * 获取客户积分详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:customer_points:query')")
//    @GetMapping(value = "/{customerId}")
//    public AjaxResult getInfo(@PathVariable("customerId") String customerId)
//    {
//        return success(customerPointsService.selectCustomerPointsByCustomerId(customerId));
//    }
//
//    /**
//     * 新增客户积分
//     */
//    @PreAuthorize("@ss.hasPermi('system:customer_points:add')")
//    @Log(title = "客户积分", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody CustomerPoints customerPoints)
//    {
//        return toAjax(customerPointsService.insertCustomerPoints(customerPoints));
//    }
//
//    /**
//     * 修改客户积分
//     */
//    @PreAuthorize("@ss.hasPermi('system:customer_points:edit')")
//    @Log(title = "客户积分", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody CustomerPoints customerPoints)
//    {
//        return toAjax(customerPointsService.updateCustomerPoints(customerPoints));
//    }
//
//    /**
//     * 删除客户积分
//     */
    @PreAuthorize("@ss.hasPermi('system:customer_points:remove')")
    @Log(title = "客户积分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable String[] customerIds)
    {
        return toAjax(customerPointsService.deleteCustomerPointsByCustomerIds(customerIds));
    }
}
